package org.bbelovic.devel.exercises;

public class RecursiveBinarySearch {

    public int binarySearch(int [] input, final int requestedItem) {
        return binarySearchInternal(0, input.length - 1, input, requestedItem);
    }

    private int binarySearchInternal(int left, int right, int [] input, int requestedItem) {
        final int middle = (left + right) / 2;
        if (left > right) return -1;
        if (input [middle] == requestedItem) return middle;
        if (requestedItem < input [middle]) {
            return binarySearchInternal(left, middle - 1, input, requestedItem);
        }
        return binarySearchInternal(middle + 1, right, input, requestedItem);
    }
}
